//
//  shoujianViewController.m
//  BAOASystem
//
//  Created by michael on 2018/2/23.
//  Copyright © 2018年 czd. All rights reserved.
//
#import "LGLSearchBar.h"

#import "shoujianxqViewController.h"
#import "shoujianViewController.h"
#import "shoujianlistTableViewCell.h"
@interface shoujianViewController ()
@property(nonatomic,strong)LGLSearchBar * searchBar;

@end

@implementation shoujianViewController

- (void)viewDidLoad {
    [super viewDidLoad];
        self.view.backgroundColor = [UIColor whiteColor];
         _searchBar = [[LGLSearchBar alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, 40) searchBarStyle:LGLSearchBarStyleDefault];
         [_searchBar setSearchBarBordeWidth:0.1 bordColor:[UIColor clearColor] bordRadius:0];
    [_searchBar searchBarTextSearchTextBlock:^(NSString *searchText) {
        NSLog(@"%@", searchText);
        if (searchText.length>0) {
    _searchstr = searchText;
        [self loadData];
}
        
    }];
    [self.view addSubview:_searchBar];
      _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 40, KScreenWidth,KScreenHeight-kTopHeight-0-kTabBarHeight-40) style:UITableViewStylePlain];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

_tableView.dataSource = self;
_tableView.delegate = self;
[self.view addSubview:_tableView];
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
 __weak typeof(self) weakSelf = self;
[self.tableView addHeaderWithCallback:^{
           _searchstr = @"";

        weakSelf.currentPage = 1;
        [weakSelf loadData]; //加载网络数据
    }];
    [self.tableView headerBeginRefreshing];
      [self.tableView addFooterWithCallback:^{
//        if ((self.currentPage - 1)*KPageSize < self.totalCounts ){
   if (weakSelf.lastPage==0 ){

//        if (weakSelf.isSearching) {
////     [self loadSearchMore];
//}
//     else {
     [weakSelf loadMore];
//     }
        }else{
            
            [weakSelf.tableView footerEndRefreshing];
            [weakSelf.view makeToast:NSLocalizedString(@"没有更多了", nil)];
            
        }
        
    }];
    // Do any additional setup after loading the view.
}
-(void)loadData{
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
NSString *website = [[NSUserDefaults standardUserDefaults] objectForKey:@"website"];
NSString *userName = [[NSUserDefaults standardUserDefaults] objectForKey:@"userName"];
NSString *password = [[NSUserDefaults standardUserDefaults] objectForKey:@"password"];

   NSString *language = [[NSUserDefaults standardUserDefaults] objectForKey:@"language"];
 
NSString *bodyStr = [NSString stringWithFormat:@"<?xml version='1.0' encoding='UTF-8'?><message><header><userName>%@</userName><password>%@</password><website>%@</website><language>%@</language><type>%@</type></header><body><username>%@</username><title>%@</title><pageNumber>1</pageNumber><pageSize>20</pageSize></body></message>",userName,password,website,language,BAtype,userName,_searchstr];

  bodyStr= [bodyStr URLEncodedString];
 NSArray * array = @[@{@"arg0":bodyStr}];
     NSString * path = [NSString stringWithFormat:@"%@%@",BAip,bMessageList];
       [HttpTools requestWithURL:path arrays:array method:@"receive" success:^(id JSON) {
        _searchBar.text = _searchstr;

      [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
  NSLog(@"%@",JSON);
     if (self.currentPage ==1)
        {
            [self.dataArray removeAllObjects];
        }
 NSDictionary *dic = JSON[@"mesgPage"];
   _lastPage = [dic[@"lastPage"]intValue];
  self.totalCounts =[dic[@"totalRow"] intValue];
 NSArray *array = dic[@"list"];
if (array.count==0)
        {
             [self.view makeToast:NSLocalizedString(@"没有数据", nil)];
        }
        _dataArray = [NSMutableArray array];
  for (NSDictionary *dic in array) {
    NSDictionary *dict = dic;
    [_dataArray addObject:dict];
}
[_tableView reloadData];
            [self.tableView headerEndRefreshing];
        self.currentPage++;
    } failure:^(NSError *error) {
            [self.tableView headerEndRefreshing];

//        [self.tableView footerEndRefreshing];
      [MBProgressHUD hideAllHUDsForView:self.view animated:YES];

    }];
}
- (void)loadMore{
NSString *website = [[NSUserDefaults standardUserDefaults] objectForKey:@"website"];
NSString *userName = [[NSUserDefaults standardUserDefaults] objectForKey:@"userName"];
NSString *password = [[NSUserDefaults standardUserDefaults] objectForKey:@"password"];

   NSString *language = [[NSUserDefaults standardUserDefaults] objectForKey:@"language"];
NSString *bodyStr = [NSString stringWithFormat:@"<?xml version='1.0' encoding='UTF-8'?><message><header><userName>%@</userName><password>%@</password><website>%@</website><language>%@</language><type>%@</type></header><body><username>%@</username><title>%@</title><pageNumber>%d</pageNumber><pageSize>20</pageSize></body></message>",userName,password,website,language,BAtype,userName,_searchstr,_currentPage];

  bodyStr= [bodyStr URLEncodedString];
 NSArray * array = @[@{@"arg0":bodyStr}];
     NSString * path = [NSString stringWithFormat:@"%@%@",BAip,bMessageList];       [HttpTools requestWithURL:path arrays:array method:@"receive" success:^(id JSON) {
NSDictionary *dic = JSON[@"mesgPage"];
 NSArray *array = dic[@"list"];
   _lastPage = [dic[@"lastPage"]intValue];
  self.totalCounts =[dic[@"totalRow"] intValue];
    for (NSDictionary *dic in array) {
    NSDictionary *dict = dic;
    [_dataArray addObject:dict];
}
[_tableView reloadData];
        [self.tableView footerEndRefreshing];

        self.currentPage++;

   } failure:^(NSError *error) {

        [self.tableView footerEndRefreshing];

    }];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

 NSDictionary *dic = _dataArray[indexPath.row];
  CGFloat height = [OAToolBox sizebyText:dic[@"content"] font:13 minsWidth:20];

    return  height+30+20+10+10;

}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}
#pragma mark UITableView dataScource
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

  static NSString *CellIdentifier = @"shoujianlistTableViewCell";

  shoujianlistTableViewCell*cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell==nil) {
        cell=[[shoujianlistTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
 NSDictionary *dic = self.dataArray[indexPath.row];
    cell.dic = dic;
    return cell;



 }
 - (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 添加一个删除按钮
    UITableViewRowAction *deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:NSLocalizedString(@"删除", nil)handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
        NSLog(@"点击了删除");
        NSDictionary *dic = _dataArray[indexPath.row];
        [self scaction:dic[@"id"]];
        }];
      deleteRowAction.backgroundColor = [UIColor colorWithHexString:@"ff5a2d"];
      return @[deleteRowAction];
      }
- (void)scaction:(NSString *)idstring{
NSString *website = [[NSUserDefaults standardUserDefaults] objectForKey:@"website"];
NSString *userName = [[NSUserDefaults standardUserDefaults] objectForKey:@"userName"];
NSString *password = [[NSUserDefaults standardUserDefaults] objectForKey:@"password"];

   NSString *language = [[NSUserDefaults standardUserDefaults] objectForKey:@"language"];
NSString *bodyStr = [NSString stringWithFormat:@"<?xml version='1.0' encoding='UTF-8'?><message><header><userName>%@</userName><password>%@</password><website>%@</website><language>%@</language><type>%@</type></header><body><id>%@</id></body></message>",userName,password,website,language,BAtype,idstring];
 bodyStr= [bodyStr URLEncodedString];
 NSArray * array = @[@{@"arg0":bodyStr}];
     NSString * path = [NSString stringWithFormat:@"%@%@",BAip,bMessageList];
 [HttpTools requestWithURL:path arrays:array method:@"deleteMessage" success:^(id JSON) {
   NSLog(@"%@",JSON);

    [self.view makeToast:JSON[@"msg"]];
//[_tableView reloadData];
[self loadData];
 } failure:^(NSError *error) {

    }];

}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

shoujianxqViewController *vc = [[shoujianxqViewController alloc]init];
vc.hidesBottomBarWhenPushed = YES;
NSDictionary *dic = _dataArray[indexPath.row];
vc.ipid = dic[@"id"];
[self.navigationController pushViewController:vc animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
